package com.share.zabbix.method;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.share.zabbix.entity.ZabbixItem;
import com.share.zabbix.request.ZabbixRequest;
import com.share.zabbix.request.ZabbixRequestBuilder;

import cn.hutool.core.util.StrUtil;

public class ZabbixItemMethod extends ZabbixBaseMethod{
	/**
	 * zabbix 根据主机号获取监控项
	 * 
	 * @param hostId
	 * @return
	 */
	public static List<ZabbixItem> queryItemsByHost(String hostId, String keyWord) {
		ZabbixRequest request = ZabbixRequestBuilder.newBuilder()
				.method("item.get")
				.extendOutput()
				.paramEntry("hostids", hostId)
				.sortByAsc("name")
				.build();
		if(!StrUtil.isEmpty(keyWord)) {
			JSONObject keyobj = new JSONObject();
			keyobj.put("key_", keyWord);
			request.putParam("search", keyobj);
		}
			
		try {
			JSONObject response = call(request);
			String result = response.getJSONArray(ZABBIX_KEY_RESULT).toJSONString();
			Gson gson = new GsonBuilder().create();
			List<ZabbixItem> itemList = gson.fromJson(result, new TypeToken<ArrayList<ZabbixItem>>() {}.getType());
			return itemList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
